<?php

namespace App\Entity\Lang;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use App\Repository\Lang\LanguageRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=LanguageRepository::class)
 */
class Language
{
    /**
     * @Common(cnName="#",listType="text",editType="text")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"lang"})
     */
    private $id;

    /**
     * @Common(cnName="语言名称",listType="text",editType="text")
     * @ORM\Column(type="string", length=255)
     * @Groups({"lang"})
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity=LangPack::class, mappedBy="language", orphanRemoval=true)
     * @Groups({"lang"})
     */
    private $packs;

    public function __construct()
    {
        $this->packs = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|LangPack[]
     */
    public function getPacks(): Collection
    {
        return $this->packs;
    }

    public function addPack(LangPack $pack): self
    {
        if (!$this->packs->contains($pack)) {
            $this->packs[] = $pack;
            $pack->setLanguage($this);
        }

        return $this;
    }

    public function removePack(LangPack $pack): self
    {
        if ($this->packs->contains($pack)) {
            $this->packs->removeElement($pack);
            // set the owning side to null (unless already changed)
            if ($pack->getLanguage() === $this) {
                $pack->setLanguage(null);
            }
        }

        return $this;
    }
}
